package com.gzu.finalwork.user.controller;

import com.gzu.finalwork.user.config.AjaxResult;
import com.gzu.finalwork.entity.user.DeliveryTrack;
import com.gzu.finalwork.user.service.DeliveryTrackService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/deliveryTrack")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
@RequiredArgsConstructor
public class DeliveryTrackController {

    private final DeliveryTrackService trackService;

    // 创建物流轨迹
    @PostMapping("/create")
    public Object createTrack(@RequestParam Long deliveryOrderId, @RequestParam String orderNumber) {
        try {
            DeliveryTrack track = trackService.createTrack(deliveryOrderId, orderNumber);
            return AjaxResult.success(track, "轨迹创建成功");
        } catch (Exception e) {
            return AjaxResult.error(-1, "轨迹创建失败，请稍后再试");
        }
    }

    // 添加轨迹记录
    @PostMapping("/addRecord")
    public Object addTrackRecord(@RequestParam Long deliveryOrderId, @RequestBody DeliveryTrack.TrackRecord record) {
        try {
            trackService.addTrackRecord(deliveryOrderId, record);
            return AjaxResult.success("轨迹记录添加成功");
        } catch (Exception e) {
            return AjaxResult.error(-1, "轨迹记录添加失败，请稍后再试");
        }
    }

    // 根据订单ID查询轨迹
    @PostMapping("/getByOrderId")
    public Object getTrackByOrderId(@RequestParam Long deliveryOrderId) {
        try {
            DeliveryTrack track = trackService.getTrackByOrderId(deliveryOrderId);
            System.out.println(track);
            if (track!= null) {
                return AjaxResult.success( "查询成功",track);
            } else {
                return AjaxResult.error(404, "未找到轨迹信息");
            }
        } catch (Exception e) {
            return AjaxResult.error(-1, "查询轨迹失败，请稍后再试");
        }
    }

    // 根据订单编号查询轨迹
    @PostMapping("/getByOrderNumber")
    public Object getTrackByOrderNumber(@RequestParam String orderNumber) {
        System.out.println("orderNumber: " + orderNumber);
        try {
            DeliveryTrack track = trackService.getTrackByOrderNumber(orderNumber);
            System.out.println(track);
            if (track!= null) {
                return AjaxResult.success( "查询成功",track);
            } else {
                return AjaxResult.error(404, "未找到轨迹信息");
            }
        } catch (Exception e) {
            return AjaxResult.error(-1, "查询轨迹失败，请稍后再试");
        }
    }

    // 获取完整跟踪信息
        @GetMapping("/getCompleteInfo")
    public Object getCompleteTrackInfo(@RequestParam String orderNumber) {
        try {
            return AjaxResult.success("查询成功",trackService.getCompleteTrackInfo(orderNumber));
        } catch (Exception e) {
            return AjaxResult.error(-1, "查询完整跟踪信息失败，请稍后再试");
        }
    }

    //删除订单的轨迹
    @PostMapping("/deleteByOrderId")
    public Object deleteTrackByOrderId(@RequestParam Long deliveryOrderId) {
        try {
            trackService.deleteTrackByOrderId(deliveryOrderId);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            return AjaxResult.error(-1, "删除失败，请稍后再试");
        }
    }

//    // 更新当前位置
//    @PostMapping("/updateLocation")
//    public Object updateLocation(@RequestParam Long deliveryOrderId, @RequestBody DeliveryTrack.Location location) {
//        try {
//            trackService.updateCurrentLocation(deliveryOrderId, location);
//            return AjaxResult.success("位置更新成功");
//        } catch (Exception e) {
//            return AjaxResult.error(-1, "位置更新失败，请稍后再试");
//        }
//    }
//
//    // 通过坐标更新位置（配送员使用）
//    @PostMapping("/updateLocationByCoordinates")
//    public Object updateLocationByCoordinates(
//            @RequestParam Long deliveryOrderId,
//            @RequestParam Double longitude,
//            @RequestParam Double latitude,
//            HttpSession session) {
//        try {
//            Long userId = (Long) session.getAttribute("userId");
//            String operator = userId != null ? "配送员ID:" + userId : "系统";
//
//            trackService.updateLocationByCoordinates(deliveryOrderId, longitude, latitude, operator);
//            return AjaxResult.success("位置更新成功");
//        } catch (Exception e) {
//            return AjaxResult.error(-1, "位置更新失败，请稍后再试");
//        }
//    }
//
//    /**
//     * 配送员接单
//     */
//    @PostMapping("/acceptOrder")
//    public Object acceptOrder(@RequestParam Long deliveryOrderId, HttpSession session) {
//        try {
//            Long delivererId = (Long) session.getAttribute("userId");
//            if (delivererId == null) {
//                return AjaxResult.error(401, "请先登录");
//            }
//
//            DeliveryTrack.TrackRecord record = new DeliveryTrack.TrackRecord();
//            record.setTimestamp(LocalDateTime.now());
//            record.setStatus(2); // 已揽件
//            record.setDescription("配送员已接单");
//            record.setOperator("配送员ID:" + delivererId);
//
//            trackService.addTrackRecord(deliveryOrderId, record);
//            return AjaxResult.success("接单成功");
//        } catch (Exception e) {
//            return AjaxResult.error(-1, "接单失败，请稍后再试");
//        }
//    }
//
//    /**
//     * 开始配送
//     */
//    @PostMapping("/startDelivery")
//    public Object startDelivery(@RequestParam Long deliveryOrderId, HttpSession session) {
//        try {
//            Long delivererId = (Long) session.getAttribute("userId");
//            if (delivererId == null) {
//                return AjaxResult.error(401, "请先登录");
//            }
//
//            DeliveryTrack.TrackRecord record = new DeliveryTrack.TrackRecord();
//            record.setTimestamp(LocalDateTime.now());
//            record.setStatus(4); // 派送中
//            record.setDescription("开始配送");
//            record.setOperator("配送员ID:" + delivererId);
//
//            trackService.addTrackRecord(deliveryOrderId, record);
//            return AjaxResult.success("开始配送成功");
//        } catch (Exception e) {
//            return AjaxResult.error(-1, "开始配送失败，请稍后再试");
//        }
//    }
//
//    /**
//     * 确认签收
//     */
//    @PostMapping("/confirmDelivery")
//    public Object confirmDelivery(@RequestParam Long deliveryOrderId, @RequestParam(required = false) String remark, HttpSession session) {
//        try {
//            Long delivererId = (Long) session.getAttribute("userId");
//            if (delivererId == null) {
//                return AjaxResult.error(401, "请先登录");
//            }
//
//            DeliveryTrack.TrackRecord record = new DeliveryTrack.TrackRecord();
//            record.setTimestamp(LocalDateTime.now());
//            record.setStatus(5); // 已签收
//            record.setDescription("包裹已签收");
//            record.setOperator("配送员ID:" + delivererId);
//            record.setRemark(remark);
//
//            trackService.addTrackRecord(deliveryOrderId, record);
//            return AjaxResult.success("确认签收成功");
//        } catch (Exception e) {
//            return AjaxResult.error(-1, "确认签收失败，请稍后再试");
//        }
//    }
}